package com.mypro.bookcity.servlet;

import com.mypro.bookcity.dao.UserDao;
import com.mypro.bookcity.entity.User;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = {"/user/login"})
public class LoginServlet extends GenericServlet {
    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        //获取表单数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //System.out.println(username+"  "+password);

        UserDao ud = new UserDao();
        User us = ud.Login(username,password);

        //System.out.println(us);

        //将登陆者的用户信息放入session
        HttpSession session = request.getSession();
        session.setAttribute("user",us);

        //判断用户登陆
        if(us!=null){
            response.sendRedirect("/pages/user/login_success.html");

        }else{
            response.sendRedirect("/pages/user/login.html");
        }

    }
}
